1704A - Two 0-1 Sequences - CodeForces Solution


constructive algorithms greedy

Please click on ads to support us..

Python Code:

def main():
    t = 1
    t = int(input())
    for _ in range(t):
        a, b = map(int, input().split())
        f = input()
        s = input()
        while f != s:
            if (len(f) <= 1):
                break
            if s[0] == '1':
                f = str(max(int(f[0]), int(f[1]))) + f[2:]
            else:
                f = str(min(int(f[0]), int(f[1]))) + f[2:]
            if len(f) < len(s):
                break
        if (f == s):
            print("YES")
        else:
            print("NO")


main()

C++ Code:

#include<bits/stdc++.h>
#define For(i,k,n)                    for (int i = k; i < n; i++)
#define RFor(i,n, k)                  for (int i = n; i >= k; i--)
#define pb                            push_back 
#define sz                            size()
#define all(vec)                      vec.begin(), vec.end()
#define int long long int
#define maxn 100000             //declared for ncrmod function
using namespace std;

//Finding NCR mod p ****************************************************************
int fac[maxn],invfact[maxn];
int powerforncr(int x,int y,int mod){
    int res=1;
    x=x%mod;
    while (y > 0){
        if (y & 1)
            res = (res%mod * x%mod) % mod;

        y = y >> 1; 
        x = (x%mod * x%mod) % mod;
    }
    return res;
}

int modInverse(int n,int mod){
    return powerforncr(n,mod-2,mod);
}

void ncrmodpPrecompute(int mod){
    fac[0]=1;
    invfact[0]=1;
    for(int i=1;i<maxn;i++){
        fac[i]=(fac[i-1]%mod * i%mod)%mod;
        invfact[i]=modInverse(fac[i],mod);
    }
}

int ncrmod(int n,int r,int mod){
    if(r<0 or n<0){
        assert(false);
    }
    
    if(n<r)
        return 0;
        
    if(r==0 or r==n)
        return 1;
        
    return (fac[n] * invfact[r] % mod) * invfact[n-r] % mod;
}

//Finding ncr ******************************************************************************
int nCr(int n,int r) {
    int p = 1;
    int k = 1;
    if (n-r<r)
        r=n-r;
    
    if(r!=0) {
        while(r) {
            k=k*r;
            p=p*n;
            int m=__gcd(p,k);
            p/=m;
            k/=m;
            r--;
            n--;
        }
    }
    else {
        p=1;
    }
    return p;
}

//Checking prime number***********************************************************
bool isPrime(int n) {
    if(n==1)
        return false;
    if (n==2 or n==3)
        return true;
    if (n%2==0 or n%3==0)
        return false;
    for(int i=5;i*i<=n;i+=6) {
        if(n%i==0 or n%(i+2)==0)
            return false;
   }
   return true;
}

//Finding sum of digits *************************************************************
int sumofdigits(int n) {
    int ans=0;
    while (n>0) {
        ans=(ans+ n%10);
        n/=10;
    }
    return ans;
}

//Finding a^b *************************************************************************
int power(int a,int b) {
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res * a);
        a = (a * a);
        b = b >> 1;
    }
    return res;
}
 
int powermod(int a, int b, int mod) {
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res * a) % mod;
        a = (a * a) % mod;
        b = b >> 1;
    }
    return res;
}
 
//Finding MEX ****************************************************************************
int findMEX(vector<int>v) {  //MEX of {1,2,3,4} is 0
    int n=v.size();
    map<int,int>mp;
    for(int i=0;i<n;i++) {
        mp[v[i]]++;
    }
    for(int i=0;i<=n+1;i++) {
        if(mp[i]==0){
        return i;
        }
    }
    return n+1;
}

//Finds all the prime numbers from 1 to n [1,n] (inclusive)
vector<int> SieveOfEratosthenes(int n){
    vector<bool>prime(n+1,true);
    for (int p=2;p*p<=n;p++) {
        if (prime[p]) {
            for (int i=p*p;i<=n;i+=p)
                prime[i]=false;
        }
    }
    
    vector<int>v;
    for(int i=2;i<=n;i++)
        if(prime[i])
            v.pb(i);

    return v;
}
    
//Finding unique prime factors  eg : For n = 30 , it returns { 2 , 3 , 5 }
vector<int> findUniquePrimeFactors(int n){
    vector<int>v;
    for(int j=2;j*j<=n;j++){
        if(n%j==0){
            v.pb(j);
            while(n%j==0){
                n/=j;
            }
        }
    }
    if(n>1)
        v.pb(n);
    return v;
}
//***************************************************Begin********************************************************

void solve(){
    int n,m;
    cin>>n>>m;
    string a,b;
    cin>>a>>b;

    int i=n-1,j=m-1;
    while(j>0){
        if(a[i]!=b[j]){
            cout<<"NO"<<endl;
            return;
        }
        i--;
        j--;
    }
    // cout<<i<<" "<<j<<endl;
    for(int k=i;k>=0;k--){
        if(a[k]==b[j]){
            cout<<"YES"<<endl;
            return;
        }
    }
    cout<<"NO"<<endl;
}

void init_code(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
}
int32_t main(){
    init_code();
    //define precomputations below
    //precomputation definations ended
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

792A - New Bus Route
221A - Little Elephant and Function
492C - Vanya and Exams
1369B - AccurateLee
892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion
946A - Partition
1068B - LCM
1692E - Binary Deque
679A - Bear and Prime 100
488A - Giga Tower
14A - Letter
1150A - Stock Arbitraging
1552A - Subsequence Permutation
1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You
988B - Substrings Sort
312A - Whose sentence is it